/*****************************************************************************
*        描述 :
*             文件描述
*        CLR版本:            3.5
*        创建者 :            蒋 光
*        个人域名:           jiangguang.net.cn
*        命名空间名称:       ECP.DataAccess.Utilities
*        文件名:             BaseDbHelper.cs
*        创建系统时间:       2012/11/2 19:07:00
*        创建年份:           2012
*
/*****************************************************************************/

namespace ECP.DataAccess.Utilities
{
    using System;
    using System.Data;
    using System.Data.Common;
    using System.IO;
    using System.Text;
    using ECP.Utilities;

    public abstract class BaseDbHelper
    {
        private DbProviderFactory _dbProviderFactory;
        public DbCommand dbCommand;
        public DbConnection dbConnection;
        public DbDataAdapter dbDataAdapter;
        public DbTransaction dbTransaction;
        public string FileName = "BaseDbHelper.txt";
        public bool inTransaction;

        protected BaseDbHelper()
        {
        }

        public virtual void AttachAdapterParameters(IDataParameter[] dataParameter)
        {
            this.AttachParameters(this.dbDataAdapter.SelectCommand, dataParameter);
        }

        public virtual void AttachParameters(IDbCommand dbCommand, IDataParameter[] dataParameter)
        {
            dbCommand.Parameters.Add(dataParameter);
        }

        public IDbTransaction BeginTransaction()
        {
            if (!this.InTransaction)
            {
                this.InTransaction = true;
                this.dbTransaction = this.dbConnection.BeginTransaction();
                this.dbCommand.Transaction = this.dbTransaction;
                this.dbDataAdapter.SelectCommand.Transaction = this.dbTransaction;
            }
            return this.dbTransaction;
        }

        public void Close()
        {
            if (this.dbConnection != null)
            {
                this.dbConnection.Close();
                this.dbConnection.Dispose();
            }
        }

        public void CommitTransaction()
        {
            if (this.InTransaction)
            {
                this.InTransaction = false;
                this.dbTransaction.Commit();
            }
        }

        public int ExecuteNonQuery(string sqlQuery)
        {
            this.dbCommand.CommandType = CommandType.Text;
            this.dbCommand.CommandText = sqlQuery;
            if (this.InTransaction)
            {
                this.dbCommand.Transaction = this.dbTransaction;
            }
            int num = this.dbCommand.ExecuteNonQuery();
            this.WriteLog(sqlQuery);
            return num;
        }

        public int ExecuteNonQuery(string sqlQuery, DbParameter[] dbParameters)
        {
            return this.ExecuteNonQuery(CommandType.Text, sqlQuery, dbParameters);
        }

        public int ExecuteNonQuery(CommandType commandType, string sqlQuery, DbParameter[] dbParameters)
        {
            return this.ExecuteNonQuery(this.dbTransaction, commandType, sqlQuery, dbParameters);
        }

        public int ExecuteNonQuery(IDbTransaction transaction, CommandType commandType, string sqlQuery, DbParameter[] dbParameters)
        {
            this.dbCommand.CommandText = sqlQuery;
            this.dbCommand.CommandType = commandType;
            if (this.dbTransaction != null)
            {
                this.dbCommand.Transaction = this.dbTransaction;
            }
            if (dbParameters != null)
            {
                this.dbCommand.Parameters.Clear();
                for (int i = 0; i < dbParameters.Length; i++)
                {
                    if (dbParameters[i] != null)
                    {
                        this.dbCommand.Parameters.Add(dbParameters[i]);
                    }
                }
            }
            int num2 = this.dbCommand.ExecuteNonQuery();
            this.dbCommand.Parameters.Clear();
            this.WriteLog(sqlQuery);
            return num2;
        }

        public int ExecuteProcedure(string procedureName)
        {
            return this.ExecuteNonQuery(CommandType.StoredProcedure, procedureName, null);
        }

        public int ExecuteProcedure(string procedureName, DbParameter[] dbParameters)
        {
            return this.ExecuteNonQuery(CommandType.StoredProcedure, procedureName, dbParameters);
        }

        public DataTable ExecuteProcedureForDataTable(string procedureName, string tableName, DbParameter[] dbParameters)
        {
            DataTable dataTable = new DataTable(tableName);
            this.Fill(dataTable, CommandType.StoredProcedure, procedureName, dbParameters);
            return dataTable;
        }

        public IDataReader ExecuteReader(string sqlQuery)
        {
            this.dbCommand.CommandType = CommandType.Text;
            this.dbCommand.CommandText = sqlQuery;
            if (this.InTransaction)
            {
                this.dbCommand.Transaction = this.dbTransaction;
            }
            DbDataReader reader = this.dbCommand.ExecuteReader();
            this.WriteLog(sqlQuery);
            return reader;
        }

        public IDataReader ExecuteReader(string sqlQuery, DbParameter[] dbParameters)
        {
            return this.ExecuteReader(CommandType.Text, sqlQuery, dbParameters);
        }

        public IDataReader ExecuteReader(CommandType commandType, string sqlQuery, DbParameter[] dbParameters)
        {
            this.dbCommand.CommandText = sqlQuery;
            this.dbCommand.CommandType = commandType;
            if (this.dbTransaction != null)
            {
                this.dbCommand.Transaction = this.dbTransaction;
            }
            if (dbParameters != null)
            {
                this.dbCommand.Parameters.Clear();
                for (int i = 0; i < dbParameters.Length; i++)
                {
                    if (dbParameters[i] != null)
                    {
                        this.dbCommand.Parameters.Add(dbParameters[i]);
                    }
                }
            }
            DbDataReader reader = this.dbCommand.ExecuteReader();
            this.dbCommand.Parameters.Clear();
            this.WriteLog(sqlQuery);
            return reader;
        }

        public object ExecuteScalar(string sqlQuery)
        {
            return this.ExecuteScalar(CommandType.Text, sqlQuery, null);
        }

        public object ExecuteScalar(string sqlQuery, DbParameter[] dbParameters)
        {
            return this.ExecuteScalar(CommandType.Text, sqlQuery, dbParameters);
        }

        public object ExecuteScalar(CommandType commandType, string sqlQuery, DbParameter[] dbParameters)
        {
            return this.ExecuteScalar(this.dbTransaction, commandType, sqlQuery, dbParameters);
        }

        public object ExecuteScalar(IDbTransaction transaction, CommandType commandType, string sqlQuery, DbParameter[] dbParameters)
        {
            this.dbCommand.CommandText = sqlQuery;
            this.dbCommand.CommandType = commandType;
            if (this.dbTransaction != null)
            {
                this.dbCommand.Transaction = this.dbTransaction;
            }
            if (dbParameters != null)
            {
                this.dbCommand.Parameters.Clear();
                for (int i = 0; i < dbParameters.Length; i++)
                {
                    if (dbParameters[i] != null)
                    {
                        this.dbCommand.Parameters.Add(dbParameters[i]);
                    }
                }
            }
            object obj2 = this.dbCommand.ExecuteScalar();
            this.dbCommand.Parameters.Clear();
            this.WriteLog(sqlQuery);
            return obj2;
        }

        public DataTable Fill(DataTable dataTable, string sqlQuery)
        {
            return this.Fill(dataTable, CommandType.Text, sqlQuery, null);
        }

        public DataSet Fill(DataSet dataSet, string sqlQuery, string tableName)
        {
            return this.Fill(dataSet, CommandType.Text, sqlQuery, tableName, null);
        }

        public DataTable Fill(DataTable dataTable, string sqlQuery, DbParameter[] dbParameters)
        {
            return this.Fill(dataTable, CommandType.Text, sqlQuery, dbParameters);
        }

        public DataSet Fill(DataSet dataSet, string sqlQuery, string tableName, DbParameter[] dbParameters)
        {
            return this.Fill(dataSet, CommandType.Text, sqlQuery, tableName, dbParameters);
        }

        public DataTable Fill(DataTable dataTable, CommandType commandType, string sqlQuery, DbParameter[] dbParameters)
        {
            this.dbDataAdapter.SelectCommand.CommandText = sqlQuery;
            this.dbDataAdapter.SelectCommand.CommandType = commandType;
            if (this.InTransaction)
            {
                this.dbDataAdapter.SelectCommand.Transaction = this.dbTransaction;
            }
            if ((dbParameters != null) && (dbParameters.Length > 0))
            {
                this.dbCommand.Parameters.Clear();
                for (int i = 0; i < dbParameters.Length; i++)
                {
                    if (dbParameters[i] != null)
                    {
                        this.dbDataAdapter.SelectCommand.Parameters.Add(dbParameters[i]);
                    }
                }
            }
            this.dbDataAdapter.Fill(dataTable);
            this.dbDataAdapter.SelectCommand.Parameters.Clear();
            this.WriteLog(sqlQuery);
            return dataTable;
        }

        public DataSet Fill(DataSet dataSet, CommandType commandType, string sqlQuery, string tableName, DbParameter[] dbParameters)
        {
            this.dbDataAdapter.SelectCommand.CommandType = commandType;
            this.dbDataAdapter.SelectCommand.CommandText = sqlQuery;
            if (this.InTransaction)
            {
                this.dbDataAdapter.SelectCommand.Transaction = this.dbTransaction;
            }
            if (dbParameters != null)
            {
                this.dbCommand.Parameters.Clear();
                for (int i = 0; i < dbParameters.Length; i++)
                {
                    if (dbParameters[i] != null)
                    {
                        this.dbDataAdapter.SelectCommand.Parameters.Add(dbParameters[i]);
                    }
                }
            }
            this.dbDataAdapter.Fill(dataSet, tableName);
            this.dbDataAdapter.SelectCommand.Parameters.Clear();
            this.WriteLog(sqlQuery);
            return dataSet;
        }

        public IDbCommand GetDbCommand()
        {
            return this.dbCommand;
        }

        public IDbConnection GetDbConnection()
        {
            return this.dbConnection;
        }

        public IDbTransaction GetDbTransaction()
        {
            return this.dbTransaction;
        }

        public IDbConnection Open()
        {
            if (string.IsNullOrEmpty(BaseConfiguration.Instance.DbConnectionString))
            {
                BaseConfiguration.Instance.GetSetting();
            }
            this.Open(BaseConfiguration.Instance.DbConnectionString);
            return this.dbConnection;
        }

        public IDbConnection Open(string connectionString)
        {
            if (!BaseCodeChange.CheckRegister())
            {
                connectionString = string.Empty;
                throw new Exception(BaseConfiguration.Instance.ExceptionInfo);
            }
            if (this.dbConnection == null)
            {
                this.dbConnection = this.dbProviderFactory.CreateConnection();
            }
            this.dbConnection.ConnectionString = connectionString;
            this.dbConnection.Open();
            this.dbCommand = this.dbProviderFactory.CreateCommand();
            this.dbCommand.Connection = this.dbConnection;
            this.dbDataAdapter = this.dbProviderFactory.CreateDataAdapter();
            this.dbDataAdapter.SelectCommand = this.dbCommand;
            return this.dbConnection;
        }

        public void RollbackTransaction()
        {
            if (this.InTransaction)
            {
                this.InTransaction = false;
                this.dbTransaction.Rollback();
            }
        }

        public void WriteLog(string sqlQuery)
        {
            this.WriteLog(DateTime.Now.ToString(BaseSystemInfo.DateFormat) + "_" + this.FileName, sqlQuery);
        }

        public void WriteLog(string fileName, string sqlQuery)
        {
            if (BaseSystemInfo.LogSQL)
            {
                string path = BaseSystemInfo.StartupPath + @"\\Log\\Query";
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                string str2 = path + @"\" + fileName;
                if (!File.Exists(str2))
                {
                    new FileStream(str2, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite).Close();
                }
                StreamWriter writer = new StreamWriter(str2, true, Encoding.Default);
                writer.WriteLine(DateTime.Now.ToString(BaseSystemInfo.DateTimeFormat) + " " + sqlQuery);
                writer.Close();
            }
        }

        public DbProviderFactory dbProviderFactory
        {
            get
            {
                if (this._dbProviderFactory == null)
                {
                    this._dbProviderFactory = DbHelperFactory.GetHelper().Instance();
                }
                return this._dbProviderFactory;
            }
        }

        public bool InTransaction
        {
            get
            {
                return this.inTransaction;
            }
            set
            {
                this.inTransaction = value;
            }
        }
    }
}